package org.jeecg.modules.wlhy.hycfg.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfg;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfgItem;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgItemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.wlhy.hycfg.service.IHyCfgService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: hy_cfg_item
 * @Author: jeecg-boot
 * @Date:   2022-09-28
 * @Version: V1.0
 */
@Api(tags="hy_cfg_item")
@RestController
@RequestMapping("/hycfg/hyCfgItem")
@Slf4j
public class HyCfgItemController extends JeecgController<HyCfgItem, IHyCfgItemService> {
	@Autowired
	private IHyCfgItemService hyCfgItemService;

	@Autowired
	private IHyCfgService hyCfgService;

	@Autowired
	private UserUtil userUtil;

	@ApiOperation(value="hy_cfg_item-分页列表查询", notes="hy_cfg_item-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCfgItem hyCfgItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if(!userUtil.isSys()){
			return Result.error("无权限操作");
		}
		String pid = hyCfgItem.getPid();
		if(StringUtils.isEmpty(pid)){
			return Result.error("pid不能为空");
		}
		QueryWrapper<HyCfgItem> queryWrapper = QueryGenerator.initQueryWrapper(hyCfgItem, req.getParameterMap());
		Page<HyCfgItem> page = new Page<HyCfgItem>(pageNo, pageSize);
		IPage<HyCfgItem> pageList = hyCfgItemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	@AutoLog(value = "hy_cfg_item-添加")
	@ApiOperation(value="hy_cfg_item-添加", notes="hy_cfg_item-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyCfgItem hyCfgItem) {
		String pid = hyCfgItem.getPid();
		if(StringUtils.isEmpty(pid)){
			return Result.error("pid不能为空");
		}
		String itemText = hyCfgItem.getItemText();
		if(StringUtils.isEmpty(itemText)){
			return Result.error("参数名不能为空");
		}

		if(!userUtil.isSys()){
			return Result.error("无权限操作");
		}

//		String itemValue = hyCfgItem.getItemValue();
//		if(StringUtils.isEmpty(itemValue)){
//			return Result.error("参数值不能为空");
//		}
		if(hyCfgItemService.checkTextExistByPid(itemText , pid)){
			return Result.error("参数名已存在");
		}
		hyCfgItemService.save(hyCfgItem);
		return Result.OK("添加成功！");
	}

	@AutoLog(value = "hy_cfg_item-编辑")
	@ApiOperation(value="hy_cfg_item-编辑", notes="hy_cfg_item-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyCfgItem hyCfgItem) {
		String pid = hyCfgItem.getPid();
		if(StringUtils.isEmpty(pid)){
			return Result.error("pid不能为空");
		}
		String itemText = hyCfgItem.getItemText();
		if(StringUtils.isEmpty(itemText)){
			return Result.error("参数名不能为空");
		}
		if(!userUtil.isSys()){
			return Result.error("无权限操作");
		}
		if(hyCfgItemService.checkTextExistByPidOutId(itemText , pid , hyCfgItem.getId())){
			return Result.error("参数名已存在");
		}
		HyCfg cfg = hyCfgService.getById(pid);
		hyCfgItemService.edit(cfg.getCfgCode() ,hyCfgItem);
		return Result.OK("编辑成功!");
	}

	@AutoLog(value = "hy_cfg_item-通过id删除")
	@ApiOperation(value="hy_cfg_item-通过id删除", notes="hy_cfg_item-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		if (!userUtil.isSys()) {
			return Result.error("无权限操作");
		}
		HyCfgItem hyCfgItem = hyCfgItemService.getById(id);
		HyCfg cfg = hyCfgService.getById(hyCfgItem.getPid());
		hyCfgItemService.deleteByCode(cfg.getCfgCode(), hyCfgItem);
		return Result.OK("删除成功!");
	}

}
